package com.example.administrator.test0211;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.StateListDrawable;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;


/**
 * Created by scenele on 17/2/12.
 * 加载activity的动画工具类
 */
public class AnimationUtils {

    /**
     * 页面动画类型
     */
    public static enum AnimationType{
        NONE,  // 无自定义动画，使用系统默认
        HORIZONTAL, // 水平切换动画
        VERTICAL,  // 垂直切换动画
    }

    public static void startActivity(Context context, Intent intent , AnimationType type){
        intent.putExtra(AppConst.BUNDLE_ANIMATION_TYPE, type.ordinal());
        context.startActivity(intent);

        if(context instanceof Activity) {
            Activity activity = (Activity)context;
            if (type == AnimationType.HORIZONTAL) {
                activity.overridePendingTransition(R.anim.slide_in_from_right, R.anim.activity_hold);
            } else if (type == AnimationType.VERTICAL) {
                activity.overridePendingTransition(R.anim.slide_in_from_bottom, R.anim.activity_hold);
            } else if(type == AnimationType.NONE){
                activity.overridePendingTransition(R.anim.activity_hold, R.anim.activity_hold);
            }
        }
    }

    public static void startActivityForResult(Activity activity, Intent intent, int requestCode, AnimationType type){
        intent.putExtra(AppConst.BUNDLE_ANIMATION_TYPE, type.ordinal());
        activity.startActivityForResult(intent, requestCode);
        if(type == AnimationType.HORIZONTAL){
            activity.overridePendingTransition(R.anim.slide_in_from_right, R.anim.activity_hold);
        }else if(type == AnimationType.VERTICAL){
            activity.overridePendingTransition(R.anim.slide_in_from_bottom, R.anim.activity_hold);
        }else if(type == AnimationType.NONE){
            activity.overridePendingTransition(R.anim.activity_hold, R.anim.activity_hold);
        }
    }

    /**
     * 给没有按下效果的有点击事件的ImageView控件添加 按下变暗的效果
     */
    public static void addViewPressEffect(final View view) {

        // 如果是ViewGroup类型，只对LinearLayout,RelativeLayout,FrameLayout做处理
        if(view instanceof ViewGroup
                && !(view instanceof LinearLayout)
                && !(view instanceof RelativeLayout)
                && !(view instanceof FrameLayout)){
            return;
        }
        if (view == null) {
            return;
        }

        if(view instanceof EditText){
            return;
        }

        view.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                // 主动设置不显示点击效果的
                if(IGNORE_TOUCH_EFFECT.equals(view.getTag())){
                    return false;
                }

                if(view.isClickable() && view.isEnabled()) {

                    if(view instanceof ViewGroup){
                        changeChildTextView((ViewGroup)view, 0);
                    }else if(view instanceof TextView){
                        setTextViewStateColor((TextView)view);
                    }

                    if (getDrawable(view) != null && getDrawable(view) instanceof StateListDrawable) {
                        return false;
                    }

                    if (view.getBackground() != null && view.getBackground() instanceof StateListDrawable) {
                        return false;
                    }

                    if (event.getAction() == MotionEvent.ACTION_DOWN) {
                        //设置滤镜
                        setColorFilter(view);

                        // 如果是没有背景的ViewGroup，则设置第一级图片子控件的按下效果
                        if(view instanceof ViewGroup && view.getBackground() == null){
                            setChildImageViewColorFilter((ViewGroup) view, true);
                        }

                    } else if (event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_CANCEL) {
                        //清除滤镜
                        clearColorFilter(view);

                        // 如果是没有背景的ViewGroup，则清除第一级图片子控件的按下效果
                        if(view instanceof ViewGroup && view.getBackground() == null){
                            setChildImageViewColorFilter((ViewGroup) view, false);
                        }
                    }
                }
                return false;
            }
        });
    }

    /**
     * 设置ViewGroup下的文本控件的按下态
     * @param parent
     * @param levelNum
     */
    private static void changeChildTextView(ViewGroup parent, int levelNum){
        if(levelNum > 8){
            return;
        }

        if(parent == null){
            return;
        }

        if(parent instanceof FrameLayout && parent.getClass().getName().startsWith("android.support.v4.app.NoSaveStateFrameLayout")){
            return;
        }

        for(int i = 0 ; i < parent.getChildCount(); i ++){
            View item = parent.getChildAt(i);
            if(item instanceof TextView){
                setTextViewStateColor((TextView)item);
            }else if(item instanceof ViewGroup){
                changeChildTextView((ViewGroup)item, levelNum + 1);
            }
        }

    }

    /**
     * 设置ViewGroup下的第一级图片子控件的的按下态
     * @param parent
     * @param set
     */
    private static void setChildImageViewColorFilter(ViewGroup parent, boolean set){

        if(parent == null){
            return;
        }

        if(parent instanceof FrameLayout && parent.getClass().getName().startsWith("android.support.v4.app.NoSaveStateFrameLayout")){
            return;
        }

        for(int i = 0 ; i < parent.getChildCount(); i ++){
            View item = parent.getChildAt(i);
            if(item instanceof ImageView){
                if(set){
                    setColorFilter(item);
                }else{
                    clearColorFilter(item);
                }
            }
        }

    }

    /**
     * 文本控件如果没有设置按下状态颜色，设置按下颜色为当前色的加深
     * @param textView
     */
    public static void setTextViewStateColor(TextView textView){
        ColorStateList colorStateList = textView.getTextColors();

        if(colorStateList != null && !colorStateList.isStateful()){
            int color = colorStateList.getDefaultColor();
            int newDefaultColor = 0xff << 24 | (Color.red(color) / 2) << 16 | (Color.green(color) / 2) << 8 | (Color.blue(color) / 2);

            int[][] stateSpecs = new int[][]{new int[]{16842912}, new int[]{16842919}, new int[]{}};
            int[] colors = new int[]{newDefaultColor, newDefaultColor, colorStateList.getDefaultColor()};
            ColorStateList newColor = new ColorStateList(stateSpecs, colors);
            textView.setTextColor(newColor);
        }
    }

    public static final String IGNORE_TOUCH_EFFECT = "ignore_touch_effect";

    /**
     * 取消该控件的按下变暗效果
     * @param view
     */
    public static void disableDefaultClickEffect(View view){
        if(view != null){
            view.setTag(IGNORE_TOUCH_EFFECT);
        }
    }

    //设置滤镜
    public static void setColorFilter(View view){
        if (getDrawable(view) != null) {
            getDrawable(view).setColorFilter(Color.GRAY, PorterDuff.Mode.MULTIPLY);
        }
        if (view.getBackground() != null) {
            view.getBackground().setColorFilter(Color.GRAY, PorterDuff.Mode.MULTIPLY);
        }
    }

    //清除滤镜
    public static void clearColorFilter(View view){
        if (getDrawable(view) != null) {
            getDrawable(view).clearColorFilter();
        }
        if (view.getBackground() != null) {
            view.getBackground().clearColorFilter();
        }
    }

    private static Drawable getDrawable(View view){
        if(view != null && view instanceof ImageView){
            return ((ImageView) view).getDrawable();
        }
        return null;
    }

}
